﻿#include "ordermanagemodel.h"

OrderManageModel::OrderManageModel(QObject *parent) :
    QAbstractTableModel(parent)
{
    m_select = D_None;
    m_header << "订单号" << "下单时间"  << "订单总额" << "订单状态" << "入住人"<< "联系电话"
                << "预定房间类型" << "预定房间数" << "入住时间" << "离店时间" << "预计到店时间";
    m_reserveAllList = new ReserveInfoList;
    m_reserveNoList = new ReserveInfoList;
}

void OrderManageModel::setCustomizeDay(const QDate& start, const QDate& end)
{
    m_startDay = start;
    m_endDay = end;
}
void OrderManageModel::setSelect(DaySelect select)
{
    m_select = select;
}

void OrderManageModel::refrushModel(DaySelect select)
{
    setSelect(select);
    int size = GlobalVars::g_reserveInfoList->size();
    m_reserveAllList->clear();
    m_reserveNoList->clear();
    QString noDealWith = "待处理";
    QString noPay = "待付款";

    switch (m_select)
    {
    case D_None:
        *m_reserveAllList = *GlobalVars::g_reserveInfoList;
        *m_reserveNoList = *GlobalVars::g_reserveInfoList;
        break;
    case D_TodayNo:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (QDate::currentDate() == date && GlobalVars::g_reserveInfoList->at(i).getState() == noDealWith)
            {
                m_reserveNoList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_YesterdayNo:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (QDate::currentDate() == date.addDays(1) && GlobalVars::g_reserveInfoList->at(i).getState() == noDealWith)
            {
                m_reserveNoList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_WeekNo:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (QDate::currentDate() <= date.addDays(7) && GlobalVars::g_reserveInfoList->at(i).getState() == noDealWith)
            {
                m_reserveNoList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_MonthNo:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (QDate::currentDate() <= date.addMonths(1) && GlobalVars::g_reserveInfoList->at(i).getState() == noDealWith)
            {
                m_reserveNoList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_CustomizeNo:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (date >= m_startDay && date <= m_endDay &&
                    GlobalVars::g_reserveInfoList->at(i).getState() == noDealWith)
            {
                m_reserveNoList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_TodayAll:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (QDate::currentDate() == date &&  GlobalVars::g_reserveInfoList->at(i).getState() != noPay)
            {
                m_reserveAllList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_YesterdayAll:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (QDate::currentDate() == date.addDays(1) &&  GlobalVars::g_reserveInfoList->at(i).getState() != noPay)
            {
                m_reserveAllList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_WeekAll:
        for (int i = 0; i < GlobalVars::g_reserveInfoList->size(); i++)
        {
            QString reserveId = GlobalVars::g_reserveInfoList->at(i).getReserve();
            QDate date = QDate::fromString(reserveId.left(8),"yyyyMMdd");
            if (QDate::currentDate() <= date.addDays(7) &&  GlobalVars::g_reserveInfoList->at(i).getState() != noPay)
            {
                m_reserveAllList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_MonthAll:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (QDate::currentDate() <= date.addMonths(1) &&  GlobalVars::g_reserveInfoList->at(i).getState() != noPay)
            {
                m_reserveAllList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    case D_CustomizeAll:
        for (int i = 0; i < size; i++)
        {
            QDate date = QDate::fromString(GlobalVars::g_reserveInfoList->at(i).getReserve().left(8),"yyyyMMdd");
            if (date >= m_startDay && date <= m_endDay &&  GlobalVars::g_reserveInfoList->at(i).getState() != noPay)
            {
                m_reserveAllList->append(GlobalVars::g_reserveInfoList->at(i));
            }
        }
        break;
    default: break;
    }


    qDebug() << "int size = GlobalVars::g_reserveInfoList->size();" << GlobalVars::g_reserveInfoList->size();
    beginResetModel();
    endResetModel();
}
int OrderManageModel::columnCount(const QModelIndex &parent) const
{
    return m_header.size();
}
int OrderManageModel::rowCount(const QModelIndex &parent) const
{
    if (m_select < D_TodayNo)
    {
        return m_reserveAllList->size();
    }
    else
    {
        return m_reserveNoList->size();
    }
}
QVariant OrderManageModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    if(role == Qt::DisplayRole)
    {
        if (m_select < D_TodayNo)
        {
            switch(index.column())
            {
                case 0: return  m_reserveAllList->at(index.row()).getReserve();
                case 1: return  m_reserveAllList->at(index.row()).getReserveTime().split("T").join(" ");
                case 2: return  m_reserveAllList->at(index.row()).getPayment();
                case 3: return  m_reserveAllList->at(index.row()).getState();
                case 4: return  m_reserveAllList->at(index.row()).getCheckInName();
                case 5: return  m_reserveAllList->at(index.row()).getCheckInTel();
                case 6: return  m_reserveAllList->at(index.row()).getHouseType();
                case 7: return  m_reserveAllList->at(index.row()).getHouseCount();
                case 8: return  m_reserveAllList->at(index.row()).getCheckInTime();
                case 9: return  m_reserveAllList->at(index.row()).getCheckOutTime();
                case 10: return m_reserveAllList->at(index.row()).getArrivalTime();
                default: return QVariant();
            }
        }
        else
        {
            switch(index.column())
            {
                case 0: return  m_reserveNoList->at(index.row()).getReserve();
                case 1: return  m_reserveNoList->at(index.row()).getReserveTime().split("T").join(" ");
                case 2: return  m_reserveNoList->at(index.row()).getPayment();
                case 3: return  m_reserveNoList->at(index.row()).getState();
                case 4: return  m_reserveNoList->at(index.row()).getCheckInName();
                case 5: return  m_reserveNoList->at(index.row()).getCheckInTel();
                case 6: return  m_reserveNoList->at(index.row()).getHouseType();
                case 7: return  m_reserveNoList->at(index.row()).getHouseCount();
                case 8: return  m_reserveNoList->at(index.row()).getCheckInTime();
                case 9: return  m_reserveNoList->at(index.row()).getCheckOutTime();
                case 10: return m_reserveNoList->at(index.row()).getArrivalTime();
                default: return QVariant();
            }
        }
    }
    return QVariant();
}
QVariant OrderManageModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role==Qt::DisplayRole && orientation==Qt::Horizontal)
    {
        return m_header[section];
    }
    return QAbstractTableModel::headerData(section,orientation,role);
}

const QString& OrderManageModel::getHouseType(int row)
{
    return m_reserveAllList->at(row).getHouseType();
}
const QString& OrderManageModel::getCheckInName(int row)
{
    return m_reserveAllList->at(row).getCheckInName();
}
const QString& OrderManageModel::getReserveID(int row)
{
    return m_reserveAllList->at(row).getReserve();
}
const QString& OrderManageModel::getOrderState(int row)
{
    return m_reserveAllList->at(row).getState();
}
